from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import gettext_lazy as _

#公告栏
class Notice(models.Model):
    STATUS_COLOR = (
        ('r', '#009688'),
        ('n', 'red'),
    )
    notice_name = models.CharField('公告内容',max_length=100)
    notice_url = models.URLField('公告网址', max_length=100,blank=True)
    is_notice = models.CharField('跳转按钮是否存在', max_length=20, blank=True)
    notice_color= models.CharField('字体颜色', max_length=8, blank=True, choices = STATUS_COLOR, default = 'r')
    class Meta:
        verbose_name = "公告"

    def __str__(self):
        #将文章标题返回
        return self.notice_name

#Banner轮播图
class Banner(models.Model):
    text_info = models.CharField('标题', max_length=50, default='')
    img = models.ImageField('轮播图', upload_to='banner/')
    link_url = models.URLField('图片链接', max_length=100)
    is_active = models.BooleanField('是否是active', default=False)

    def __str__(self):
        return self.text_info

    class Meta:
        verbose_name = '轮播图'
        verbose_name_plural = '轮播图'

#友情链接
class Link(models.Model):
    name = models.CharField('链接名称', max_length=20)
    linkurl = models.URLField('网址',max_length=100)

    def __str__(self):
        return self.name
    class Meta:
        verbose_name = '友情链接'
        verbose_name_plural = '友情链接'

#站点设置
class BlogSettings(models.Model):
    """blog的配置"""
    site_name = models.CharField("网站名称",max_length=200,null=False,blank=False,default='')
    site_logo = models.ImageField('网站logo',upload_to='logos/%Y%m%d/',default='', blank=True)
    site_description = models.TextField("网站描述",max_length=1000,null=False,blank=False,default='')
    site_seo_description = models.TextField("网站SEO描述", max_length=1000, null=False, blank=False, default='')
    site_QQ = models.CharField("QQ",max_length=100,null=False,default='暂未添加')
    site_email = models.CharField("邮箱",max_length=100,null=False,default='暂未添加')
    site_wetch = models.CharField("微信",max_length=100,null=False,default='暂未添加')
    site_keywords = models.TextField("网站关键字",max_length=1000,null=False,blank=False,default='')
    article_sub_length = models.IntegerField("文章摘要长度", default=300)
    global_header = models.TextField("公共头部", null=True, blank=True, default='')
    global_footer = models.TextField("公共尾部", null=True, blank=True, default='')
    beian_code = models.CharField('备案号',max_length=2000,null=True,blank=True,default='')
    analytics_code = models.TextField("网站统计代码",max_length=1000,null=False,blank=True,default='')
    show_gongan_code = models.BooleanField('是否显示公安备案号', default=False, null=False)
    gongan_beiancode = models.TextField('公安备案号',max_length=2000,null=True,blank=True,default='')


    def __str__(self):
        return self.site_name

    class Meta:
        verbose_name = '网站配置'
        verbose_name_plural = '网站配置'

    def clean(self):
        if BlogSettings.objects.exclude(id=self.id).count():
            raise ValidationError(_('只能有一个配置'))
